//
//  RedPacketDetailViewController.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/9/13.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "RedPacketDetailViewController.h"
#import "ReflectDetailViewController.h"

@interface RedPacketDetailViewController()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _nextKey;
    NSMutableArray *_dataArr;
    UITableView *_tab;
}

@end

@implementation RedPacketDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _dataArr = [NSMutableArray array];
    _nextKey = 0;
    [self getData:_nextKey];
    [self initUI];
}
- (void)initUI
{
    if (_isReflect) {
        self.title = @"提现记录";
    }else{
        self.title = @"中奖记录";
    }
    [self.view addSubview:({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, WIDTH/3, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        if (_isReflect) {
            
            label.text = @"提现时间";
        }else{
            label.text = @"活动名称";
        }
        
        label;
    })];
    [self.view addSubview:({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH*2/3, 64, WIDTH/3, 40)];
        if (_isReflect) {
            label.text = @"提现状态";
        }else{
            label.text = @"中奖金额";
        }
        
        label.textAlignment = NSTextAlignmentCenter;
        label;
    })];
    [self.view addSubview:({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/3, 64, WIDTH/3, 40)];
        if (_isReflect) {
            label.text = @"提现金额";
        }else{
            label.text = @"中奖时间";
        }
        
        label.textAlignment = NSTextAlignmentCenter;
        label;
    })];
    
    [self.view addSubview:({
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 104, WIDTH-30, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        line;
    })];
    
    _tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 105, WIDTH, HEIGHT-105) style:UITableViewStylePlain];
    [self.view addSubview:_tab];
    _tab.delegate = self;
    _tab.dataSource = self;
    [_tab delsetExtraline];
    _tab.header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_dataArr removeAllObjects];
        _nextKey = 0;
        [self getData:_nextKey];
    }];
    _tab.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getData:_nextKey];
    }];
}
- (void)getData:(NSInteger)nextKey
{
    if (nextKey == NSNotFound) {
        [_tab.header endRefreshing];
        [_tab.footer endRefreshing];
        [self.view Message:@"亲，没有记录了"];
        return;
    }
    
    if (_isReflect) {
        [MyRedPacket reflectRecord:^(NSDictionary *response) {
            [_tab.header endRefreshing];
            [_tab.footer endRefreshing];
            
            if ([[response objectForKey:@"status"]integerValue]) {
                
                if ([[response objectForKey:@"next_key"]integerValue]) {
                    _nextKey = [[response objectForKey:@"next_key"]integerValue];
                }else{
                    _nextKey = NSNotFound;
                }
                
                NSArray *arr = [response objectForKey:@"wlDetailList"];
                for (NSDictionary *dic in arr) {
                    [_dataArr addObject:dic];
                }
                [_tab reloadData];
                if (!_dataArr.count) {
                    noMessage(_tab, @"暂时没有数据");
                }
            }
        } vc:self nextKey:nextKey];
    }else{
        
        [MyRedPacket redPacketRecord:^(NSDictionary *response) {
            
            [_tab.header endRefreshing];
            [_tab.footer endRefreshing];
            
            if ([[response objectForKey:@"status"]integerValue]) {
                
                if ([[response objectForKey:@"next_key"]integerValue]) {
                    _nextKey = [[response objectForKey:@"next_key"]integerValue];
                }else{
                    _nextKey = NSNotFound;
                }
                
                
                NSArray *arr = [response objectForKey:@"dbDetailList"];
                for (NSDictionary *dic in arr) {
                    [_dataArr addObject:dic];
                }
                [_tab reloadData];
                if (!_dataArr.count) {
                    noMessage(_tab, @"暂时没有数据");
                }

            }
        }vc:self nextKey:nextKey];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *name,*gain_time,*money;
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        [cell.contentView addSubview:({
            name = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH/3, 40)];
            name.textColor = [UIColor darkGrayColor];
            name.textAlignment = NSTextAlignmentCenter;
            name.font = [UIFont systemFontOfSize:14];
            name;
        })];
        [cell.contentView addSubview:({
            gain_time = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, 40)];
            gain_time.textColor = [UIColor darkGrayColor];
            gain_time.font = [UIFont systemFontOfSize:14];
            gain_time.textAlignment = NSTextAlignmentCenter;
            gain_time;
        })];
        [cell.contentView addSubview:({
            money = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH*2/3, 0, WIDTH/3, 40)];
            money.textAlignment = NSTextAlignmentCenter;
            money.textColor = [UIColor redColor];
            money;
        })];
        
        cell.separatorInset=UIEdgeInsetsMake(0,15,0,15);
    }
    NSDictionary *dic = _dataArr[indexPath.row];
    
    if (_isReflect) {
        
        name.text = [MyDateformater formater:dic[@"time"]];
        gain_time.text = dic[@"money"];
        if ([dic[@"status1"]integerValue]==0) {
            money.textColor = RGBcolor(106, 195, 9);
            money.text = @"提现审核中";
        }else if ([dic[@"status1"]integerValue]==1){
        
            money.textColor = [UIColor darkGrayColor];
            money.text = @"提现完成";
        }else{
            money.textColor = [UIColor redColor];
            money.text = @"提现异常";
        }
        
        
    }else{
        
        name.text = dic[@"name"];
        gain_time.text = [MyDateformater formater:dic[@"gain_time"]];
        money.text = dic[@"money"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_isReflect) {
        NSDictionary *dic = _dataArr[indexPath.row];
        ReflectDetailViewController *vc = [[ReflectDetailViewController alloc]init];
        vc.data = dic;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
